]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
dbusErrors: Add utility module for handling remote errors
authorFlorian Müllner <fmuellner@gnome.org>
Wed, 31 Jan 2024 15:28:24 +0000 (16:28 +0100)
committerFlorian Müllner <fmuellner@gnome.org>
Wed, 31 Jan 2024 19:58:31 +0000 (20:58 +0100)
We sometimes return custom D-Bus errors. Right now those errors
are ad-hoc, which means receivers don't have a proper way of
checking whether a thrown GLib.Error corresponds to a particular
remote error.

In order to change that, we should define proper error enums and
domains, and register them with GDBus, so that there is an automatic
mapping between D-Bus errors and GLib.Errors.

The new module doesn't export any domains or enums yet, but provides
internal tooling to make these exports more convenient.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/3159>

js/js-resources.gresource.xml
js/misc/dbusErrors.js [new file with mode: 0644]

index f6b8b7e78fcc4b83412fda822ed6c2450ccbb339..ad83acc30c7e691b5896622aae51d5f465c0ab83 100644 (file)
@@ -17,6 +17,7 @@
     <file>misc/animationUtils.js</file>
     <file>misc/config.js</file>
     <file>misc/dateUtils.js</file>
+    <file>misc/dbusErrors.js</file>
     <file>misc/dbusUtils.js</file>
     <file>misc/dependencies.js</file>
     <file>misc/errorUtils.js</file>
diff --git a/js/misc/dbusErrors.js b/js/misc/dbusErrors.js
new file mode 100644 (file)
index 0000000..5e4d62e
--- /dev/null
@@ -0,0 +1,29 @@
+/* eslint-disable no-unused-vars */
+import GLib from 'gi://GLib';
+import Gio from 'gi://Gio';
+
+function decamelcase(str, sep) {
+    return str.replace(/(.)([A-Z])/g, `$1${sep}$2`);
+}
+
+function registerErrorDomain(domain, errorNames, prefix = 'org.gnome.Shell') {
+    const domainName =
+        `shell-${decamelcase(domain, '-').toLowerCase()}-error`;
+    const quark = GLib.quark_from_string(domainName);
+
+    for (const [code, name] of errorNames.entries()) {
+        Gio.dbus_error_register_error(quark,
+            code, `${prefix}.${domain}.Error.${name}`);
+    }
+    return quark;
+}
+
+function createErrorEnum(errorNames) {
+    const obj = {};
+
+    for (const [code, name] of errorNames.entries()) {
+        const propName = decamelcase(name, '_').toUpperCase();
+        obj[propName] = code;
+    }
+    return obj;
+}